package per.ace.jpa.web;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import per.ace.jpa.entity.Record;
import per.ace.jpa.listener.RecordListener;
import per.ace.jpa.service.RecordService;

import java.io.IOException;

/**
 * @program: springbootDemo
 * @description: 上传接口
 * @author: ace
 * @Date: 2020-03-21 19:08
 */
@RestController
@RequestMapping("record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    /**
     * 文件上传
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link Record}
     * <p>
     * 2. 由于默认异步读取excel，所以需要创建excel一行一行的回调监听器，参照{@link RecordListener}
     * <p>
     * 3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Record.class, new RecordListener(recordService)).sheet().doRead();
        return "success";
    }
}
